#------------------------------------------#
# !python3
# coding:utf-8
# Author: Terry
# Descpiton: 利用requests 和bs4 模块爬取博客标题,并写入文件
#------------------------------------------#

import requests
from bs4 import BeautifulSoup

#爬虫步骤
    #确认目标范围
    #获取目标地址
    #发起请求 
    #获取数据   
    #解析数据 
    #整理数据 
    #保存数据

#构造地址：
base_url="http://www.cnblogs.com/ALittleBee/default.html?page=" 


user_agent="Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0;)"
headers={"User-Agent":user_agent}

for i in range(1,10):
    urlf=base_url+str(i)
    res=requests.get(urlf,headers=headers) # 包装请求

    soup=BeautifulSoup(res.text, "lxml")
    titles=soup.find_all('a',{'class':'postTitle2'})
    for item in titles:
        title=item.text.strip()
        link=item['href']
    with open('f:\\blogs.txt','a+') as f:
        f.write(title+','+link+'\n')
print ('job done')